package com.huirui.server.pojo.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.util.List;

@Data
@Schema(description = "商品信息DTO")
@ToString
public class ProductDto {

    @Schema(description = "商品Id")
    private Long id;

    @NotBlank(message = "商品名称不能为空")
    @Schema(description = "商品名称", example = "医用外科口罩", requiredMode = Schema.RequiredMode.REQUIRED)
    @Size(min = 2, max = 50, message = "商品名称长度必须在2-50个字符之间")
    private String productName;

    @Size(max = 50, message = "商品提示信息长度不能超过50个字符")
    @Schema(description = "商品提示信息", example = "一次性使用，请勿重复使用")
    private String tip;

    @NotEmpty(message = "商品分类ID列表不能为空")
    @Schema(description = "商品分类ID列表", example = "[1, 3, 5]")
    private List<Long> categoryIds;

    @NotBlank(message = "商品主图不能为空")
    @Schema(description = "商品主图URL", example = "https://example.com/images/product/main.jpg", requiredMode = Schema.RequiredMode.REQUIRED)
    private String productImage;

    @NotNull(message = "商品状态不能为空")
    @Min(value = 0, message = "商品状态不能小于0")
    @Max(value = 2, message = "商品状态不能大于2")
    @Schema(description = "商品状态(0-上架, 1-下架)", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer status;

    @NotNull(message = "商品权重不能为空")
    @Min(value = 1, message = "商品权重不能小于1")
    @Max(value = 100, message = "商品权重不能大于100")
    @Schema(description = "商品权重", example = "500", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long weight;

    @NotEmpty(message = "商品规格列表不能为空")
    @Schema(description = "商品规格列表", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<ProductSpecificationDto> productSpecificationDtoList;

    @NotNull(message = "库存计数方式不能为空")
    @Min(value = 0, message = "库存计数方式不能小于0")
    @Max(value = 1, message = "库存计数方式不能大于1")
    @Schema(description = "库存计数方式(0-按个, 1-按重量)", example = "0", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer inventoryCountingMethod;

    @NotEmpty(message = "商品轮播图不能为空")
    @Schema(description = "商品轮播图URL列表", example = "[\"https://example.com/images/banner1.jpg\", \"https://example.com/images/banner2.jpg\"]", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<String> bannerImages;

    @NotEmpty(message = "商品轮播图不能为空")
    @Schema(description = "商品详情图URL列表", example = "[\"https://example.com/images/detail1.jpg\", \"https://example.com/images/detail2.jpg\"]")
    private List<String> detailImages;

    @Size(min = 2, max = 200, message = "生产企业名称长度必须在2-200个字符之间")
    @Schema(description = "生产企业（可为空，若填写则长度需在2-200字符之间）",
            example = "XX医疗器械有限公司")
    private String manufacturingEnterprise;

    @Schema(description = "商品品牌", example = "XX品牌")
    private Long brandId;

    @Size(max = 200, message = "注册号长度不能超过200个字符")
    @Schema(description = "商品注册号", example = "国药准字XXXX2023XXXX")
    private String registrationNumber;

    @Size(max = 200, message = "医保编号长度不能超过200个字符")
    @Schema(description = "医保编号", example = "YBXXXX2023XXXX")
    private String medicalInsuranceNumber;

    @Schema(description = "注册证照片URL", example = "https://example.com/images/registration.jpg")
    private String registrationIdPhoto;

    @Schema(
            description = "生产日期",
            example = "2023-10-16",  // 提供示例值，方便前端参考
            type = "string",  // 明确数据类型
            format = "date",  // 声明格式为日期（与pattern对应）
            requiredMode = Schema.RequiredMode.REQUIRED   // 标记是否为必填字段（根据实际业务调整）
    )
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate manufacturingDate;

    @Schema(
            description = "有效期日期",
            example = "2023-10-16",  // 提供示例值，方便前端参考
            type = "string",  // 明确数据类型
            format = "date",  // 声明格式为日期（与pattern对应）
            requiredMode = Schema.RequiredMode.REQUIRED   // 标记是否为必填字段（根据实际业务调整）
    )
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate expiryDate;

    @Min(value = 0, message = "初始销量不能小于0")
    @Schema(description = "初始销量", example = "0", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long initialSalesVolume;

    @Schema(description = "商品包装", requiredMode = Schema.RequiredMode.REQUIRED)
    @Size(min = 2, max = 50, message = "商品包装的长度不能低于2个字符，不能长于50个字符")
    private String productPackaging;


    @Data
    @Schema(description = "商品规格DTO")
    public static class ProductSpecificationDto {

        private Long id;

        @NotBlank(message = "规格名称不能为空")
        @Size(max = 50, message = "规格名称长度不能超过50个字符")
        @Schema(description = "规格名称", example = "50片/盒", requiredMode = Schema.RequiredMode.REQUIRED)
        private String productSpecificationName;

        @Schema(description = "规格图片URL", example = "https://example.com/images/spec1.jpg")
        private String productSpecificationImage;

        @NotNull(message = "折扣价不能为空")
        @DecimalMin(value = "0.01", message = "折扣价不能小于0.01")
        @Digits(integer = 10, fraction = 2, message = "折扣价最多10位整数，2位小数")
        @Schema(description = "折扣价", example = "19.90", requiredMode = Schema.RequiredMode.REQUIRED)
        private Double discountPrice;

        @NotNull(message = "原价不能为空")
        @DecimalMin(value = "0.01", message = "原价不能小于0.01")
        @Digits(integer = 10, fraction = 2, message = "原价最多10位整数，2位小数")
        @Schema(description = "原价", example = "29.90", requiredMode = Schema.RequiredMode.REQUIRED)
        private Double originalPrice;

        @NotNull(message = "销量不能为空")
        @Min(value = 0, message = "销量不能为负数") // 添加此注解限制不能为负数
        @Schema(description = "销量", example = "100", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long saleCount;

        @NotNull(message = "规格权重权重不能为空")
        @Min(value = 1, message = "规格权重不能小于1")
        @Max(value = 100, message = "规格权重不能大于100")
        @Schema(description = "商品权重", example = "500", requiredMode = Schema.RequiredMode.REQUIRED)
        private Integer sort;

        @NotNull(message = "库存数量不能为空")
        @Min(value = 0, message = "库存数量不能小于0")
        @Schema(description = "库存数量", example = "1000", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long stock;
    }
}
